<template>
	<view class="container">
		<u-form ref="uForm" label-position="left" label-width="auto" :model="formData" :rules="rules" :errorType="['message']">
			<view class="form-wrapper">
				<u-form-item label="课程类型" prop="type" right-icon="arrow-right" :right-icon-style="{color:'#D7D7D7',fontSize:'24rpx'}">
					<u-input type="select" placeholder="请选择" :placeholder-style="{color: '#999999'}" :clearable="false" v-model="formData.type" :disabled="true" @click="typeShow=true"></u-input>
				</u-form-item>
				<u-form-item label="课程名称" prop="name" right-icon="arrow-right" :right-icon-style="{color:'#D7D7D7',fontSize:'24rpx'}">
					<u-input type="select" placeholder="请选择" :placeholder-style="{color: '#999999'}" :clearable="false" v-model="formData.name" :disabled="true" @click="nameShow=true"></u-input>
				</u-form-item>
				<u-form-item label="老师" prop="teacher_name" right-icon="arrow-right" :right-icon-style="{color:'#D7D7D7',fontSize:'24rpx'}" >
					<u-input type="select" placeholder="请选择" :placeholder-style="{color: '#999999'}" :clearable="false" v-model="formData.teacher_name" :disabled="true" @click="teacherShow=true"></u-input>
				</u-form-item>
				<u-form-item label="课程时长（分）" prop="course_minutes">
					<u-input type="number" placeholder="必填" :placeholder-style="{color: '#999999'}" :clearable="false" :disabled="true" v-model="formData.course_minutes"></u-input>
				</u-form-item>
				<u-form-item label="课程颜色" prop="color" right-icon="arrow-right" :right-icon-style="{color:'#D7D7D7',fontSize:'24rpx'}">
					<view class="u-input__input" :style="{color:formData.color?'#333333':'#999999'}" ><text v-if="formData.color" class="color" :style="{background: formData.color}"></text><text v-else>请选择</text></view>
				</u-form-item>
				<u-form-item label="课程难度" prop="degree" style="display:block;background:#FBFBFB;">
					<view class="u-input__input">
						<u-rate :current="formData.degree" :disabled="true" inactive-icon="star-fill" inactive-color="#DFDFDF" active-color="#F5C822" size="24" gutter="6"></u-rate>
					</view>
				</u-form-item>
			</view>
			<view class="form-wrapper">
				<u-form-item label="上课教室" prop="room" right-icon="arrow-right" :right-icon-style="{color:'#D7D7D7',fontSize:'24rpx'}" :border-bottom="false" >
					<u-input type="select" placeholder="请选择" :placeholder-style="{color: '#999999'}" :clearable="false" v-model="formData.room" @click="roomShow=true"></u-input>
				</u-form-item>
				<!-- <u-form-item label="上课老师" prop="teacher_name" right-icon="arrow-right" :right-icon-style="{color:'#D7D7D7',fontSize:'24rpx'}" :border-bottom="false" >
					<u-input type="select" placeholder="请选择" :placeholder-style="{color: '#999999'}" :clearable="false" v-model="formData.teacher_name" @click="teacherShow=true"></u-input>
				</u-form-item> -->
				<u-form-item label="容纳人数（人）" prop="contain">
					<u-input type="number" placeholder="必填" :placeholder-style="{color: '#999999'}" :clearable="false" v-model="formData.contain"></u-input>
				</u-form-item>
			</view>
			<view class="form-wrapper">
				<u-form-item :label="`${item.week} ${item.mmdd}`" v-for="(item,index) in classTime" :key="index">
					<view class="u-input__input">
						<u-icon name="minus-circle-fill" color="#D90000" size="28" :label="item.time" label-size="26" label-color="#333" margin-right="6" label-pos="left" @click="deleteTime(index)"></u-icon>
					</view>
				</u-form-item>
			</view>
		</u-form>
		<view class="order-footer">
			<view class="order-footer__btn" v-if="addStepShow">
				<u-button :custom-style="wd_button_plain" shape="circle" :ripple="true" :plain="true" @click="dateShow=true">添加上课时间</u-button>
			</view>
			<view class="order-footer__btn">
				<u-button :custom-style="wd_button_primary" shape="circle" :ripple="true" @click="submit">保存</u-button>
			</view>
			
			<view class="order-footer__btn" v-if="arrangement_id>0">
				<u-button :custom-style="{height: '90rpx',background: 'rgba(0,0,0,0)',fontSize: '32rpx',color: '#666666'}" shape="circle" :ripple="true" @click="del_step">删除</u-button>
			</view>
		</view>
		
		<u-action-sheet v-model="typeShow" :list="typeList" @click="typeConfirm"></u-action-sheet>
		<u-select mode="single-column" title="请选择课程名称" v-model="nameShow" :list="nameList" @confirm="nameConfirm"></u-select>
		<u-select mode="single-column" title="请选择上课教室" v-model="roomShow" :list="roomList" @confirm="roomConfirm"></u-select>
		<u-select mode="single-column" title="请选择上课老师" v-model="teacherShow" :list="teacherList" @confirm="teacherConfirm"></u-select>
		<u-popup v-model="colorShow" mode="bottom">
			<view class="color-wrapper">
				<view class="color-wrapper__title">
					<view style="color: #606266;" @click="colorShow=false">取消</view>
					<view>请选择课程颜色</view>
					<view style="color: #2979ff;" @click="colorConfirm">确定</view>
				</view>
				<view class="color-wrapper__body">
					<text v-for="(item,index) in colorList" :key="index" :class="{'active':item.isSelect}" :style="{background: item.color}" @click="colorSelect(index)"></text>
				</view>
			</view>
		</u-popup>
		<u-calendar v-if="dateShow" mode="date" toolTip="请选择上课日期" :min-date="dateToday" max-date="2050-01-01" v-model="dateShow" @change="dateChange"></u-calendar>
		<!-- <u-select v-if="timeShow" mode="single-column" title="请选择上课时间" v-model="timeShow" :list="timeList" @confirm="timeConfirm"></u-select> -->
		<u-select v-if="timeShow" mode="mutil-column" title="请选择上课时间" v-model="timeShow" :list="timeRegion" @confirm="timeConfirm"></u-select>
		
		<!-- <u-picker mode="time" title="请选择开始时间" v-model="timeStartShow" :params="timeParams" @confirm="timeStartConfirm"></u-picker>
		<u-picker mode="time" title="请选择结束时间" v-model="timeEndShow" :params="timeParams" @confirm="timeEndConfirm"></u-picker> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				arrangement_id:0,
				addStepShow:true,
				formData: {
					type: '', //课程类型,
					type_id:0,
					name: '', //课程名称
					course_id:0,
					course_minutes: '', //课程时长（分）
					room: '', //上课教室
					classroom_id:0,
					contain: '', //容纳人数（人）
					color: '', //课程颜色
					degree:5,
					teacher_name:'',
					teacher_id:0,
				},
				rules: {
					type: [{ required: true, trigger: 'change', message: '请选择课程类型' }],
					name: [{ required: true, trigger: 'change', message: '请选择课程名称' }],
					teacher_name: [{ required: true, trigger: 'change', message: '请选择教练' }],
					// duration: [{ required: true, trigger: ['change','blur'], message: '请填写课程时长' }],
					room: [{ required: true, trigger: 'change', message: '请选择上课教室' }],
					// contain: [{ required: true, trigger: ['change','blur'], message: '请填写容纳人数' }],
					//color: [{ required: true, trigger: 'change', message: '请选择课程颜色' }],
					//limit: [{ required: true, trigger: 'change', message: '请选择开课人数限制' }],
				},
				typeShow: false,
				typeList: [],
				nameShow: false,
				nameList: [],
				roomShow: false,
				roomList: [],
				colorShow: false,
				colorList: [
					// { color: '#AD649B', isSelect: false },
					// { color: '#CF9FC7', isSelect: false },
					// { color: '#8C9498', isSelect: false },
					// { color: '#978088', isSelect: false },
				
				],
				dateShow: false,
				dateToday: '1950-01-01',
				dateValue: '',
				timeShow: false,
				timeList: [
					//{ value: 0, label: '9:00(09:00:00-10:00:00)' },
				
				],
				timeRegion: [
					[{value:'00',label:'00'},{value:'01',label:'01'},{value:'02',label:'02'},{value:'03',label:'03'},{value:'04',label:'04'},{value:'05',label:'05'},{value:'06',label:'06'},{value:'07',label:'07'},{value:'08',label:'08'},{value:'09',label:'09'},{value:'10',label:'10'},{value:'11',label:'11'},{value:'12',label:'12'},{value:'13',label:'13'},{value:'14',label:'14'},{value:'15',label:'15'},{value:'16',label:'16'},{value:'17',label:'17'},{value:'18',label:'18'},{value:'19',label:'19'},{value:'20',label:'20'},{value:'21',label:'21'},{value:'22',label:'22'},{value:'23',label:'23'}],
					[{value:':',label:':'}],
					[{value:'00',label:'00'},{value:'01',label:'01'},{value:'02',label:'02'},{value:'03',label:'03'},{value:'04',label:'04'},{value:'05',label:'05'},{value:'06',label:'06'},{value:'07',label:'07'},{value:'08',label:'08'},{value:'09',label:'09'},{value:'10',label:'10'},{value:'11',label:'11'},{value:'12',label:'12'},{value:'13',label:'13'},{value:'14',label:'14'},{value:'15',label:'15'},{value:'16',label:'16'},{value:'17',label:'17'},{value:'18',label:'18'},{value:'19',label:'19'},{value:'20',label:'20'},{value:'21',label:'21'},{value:'22',label:'22'},{value:'23',label:'23'},{value:'24',label:'24'},{value:'25',label:'25'},{value:'26',label:'26'},{value:'27',label:'27'},{value:'28',label:'28'},{value:'29',label:'29'},{value:'30',label:'30'},{value:'31',label:'31'},{value:'32',label:'32'},{value:'33',label:'33'},{value:'34',label:'34'},{value:'35',label:'35'},{value:'36',label:'36'},{value:'37',label:'37'},{value:'38',label:'38'},{value:'39',label:'39'},{value:'40',label:'40'},{value:'41',label:'41'},{value:'42',label:'42'},{value:'43',label:'43'},{value:'44',label:'44'},{value:'45',label:'45'},{value:'46',label:'46'},{value:'47',label:'47'},{value:'48',label:'48'},{value:'49',label:'49'},{value:'50',label:'50'},{value:'51',label:'51'},{value:'52',label:'52'},{value:'53',label:'53'},{value:'54',label:'54'},{value:'55',label:'55'},{value:'56',label:'56'},{value:'57',label:'57'},{value:'58',label:'58'},{value:'59',label:'59'}],
					[{value:'-',label:'至'}],
					[{value:'00',label:'00'},{value:'01',label:'01'},{value:'02',label:'02'},{value:'03',label:'03'},{value:'04',label:'04'},{value:'05',label:'05'},{value:'06',label:'06'},{value:'07',label:'07'},{value:'08',label:'08'},{value:'09',label:'09'},{value:'10',label:'10'},{value:'11',label:'11'},{value:'12',label:'12'},{value:'13',label:'13'},{value:'14',label:'14'},{value:'15',label:'15'},{value:'16',label:'16'},{value:'17',label:'17'},{value:'18',label:'18'},{value:'19',label:'19'},{value:'20',label:'20'},{value:'21',label:'21'},{value:'22',label:'22'},{value:'23',label:'23'}],
					[{value:':',label:':'}],
					[{value:'00',label:'00'},{value:'01',label:'01'},{value:'02',label:'02'},{value:'03',label:'03'},{value:'04',label:'04'},{value:'05',label:'05'},{value:'06',label:'06'},{value:'07',label:'07'},{value:'08',label:'08'},{value:'09',label:'09'},{value:'10',label:'10'},{value:'11',label:'11'},{value:'12',label:'12'},{value:'13',label:'13'},{value:'14',label:'14'},{value:'15',label:'15'},{value:'16',label:'16'},{value:'17',label:'17'},{value:'18',label:'18'},{value:'19',label:'19'},{value:'20',label:'20'},{value:'21',label:'21'},{value:'22',label:'22'},{value:'23',label:'23'},{value:'24',label:'24'},{value:'25',label:'25'},{value:'26',label:'26'},{value:'27',label:'27'},{value:'28',label:'28'},{value:'29',label:'29'},{value:'30',label:'30'},{value:'31',label:'31'},{value:'32',label:'32'},{value:'33',label:'33'},{value:'34',label:'34'},{value:'35',label:'35'},{value:'36',label:'36'},{value:'37',label:'37'},{value:'38',label:'38'},{value:'39',label:'39'},{value:'40',label:'40'},{value:'41',label:'41'},{value:'42',label:'42'},{value:'43',label:'43'},{value:'44',label:'44'},{value:'45',label:'45'},{value:'46',label:'46'},{value:'47',label:'47'},{value:'48',label:'48'},{value:'49',label:'49'},{value:'50',label:'50'},{value:'51',label:'51'},{value:'52',label:'52'},{value:'53',label:'53'},{value:'54',label:'54'},{value:'55',label:'55'},{value:'56',label:'56'},{value:'57',label:'57'},{value:'58',label:'58'},{value:'59',label:'59'}],
				],
				classTime: [],
				teacherShow:false,
				teacherList:[],
				
				timeStartShow: false,
				timeStartValue: '',
				timeEndShow: false,
				timeEndValue: '',
				timeParams: {
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: false
				},
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
			let date  = new Date(),
				year  = date.getFullYear(),
				month = date.getMonth() + 1,
				day   = date.getDate();
			month = month<10?'0'+month:month;
			day   = day<10?'0'+day:day;
			this.dateToday = year+'-'+month+'-'+day
		},
		onLoad(options){
			if(options.arrangement_id || options.arrangement_id > 0){
				this.arrangement_id = options.arrangement_id;
				this.getDetail();
				this.addStepShow = false;
			}
			this.fetchCategory();
			this.getClassroom();
			this.getTeachers();
		},
		methods: {
			//获取分类
			fetchCategory(){
				let shop = this.$core.getCurrentShop();
				let shop_id = shop.id;
				this.$core.get({url:'fitness.common/category',data:{shop_id:shop_id},success:(ret)=>{
					for(var i=0;i<ret.data.length;i++){
						ret.data[i].text = ret.data[i].name
					}	
					 this.typeList = ret.data;
				}});
			},
			
			//获取教室
			getClassroom(){
				var that = this;
				let shop = this.$core.getCurrentShop();
				let shop_id = shop.id;
				this.$core.get({url:'fitness.common/classroom',data:{shop_id:shop_id},success:(ret)=>{
					for(var i=0;i<ret.data.length;i++){
						ret.data[i].value = ret.data[i].id;
						ret.data[i].label = ret.data[i].name;
					}				
					that.roomList = ret.data;
				}});
			},
			
			//获取老师
			getTeachers(){
				var that = this;
				let shop = this.$core.getCurrentShop();
				let shop_id = shop.id;
				this.$core.get({url:'fitness.common/range_teacher',data:{shop_id:shop_id},success:(ret)=>{
					for(var i=0;i<ret.data.length;i++){
						ret.data[i].value = ret.data[i].id;
						ret.data[i].label = ret.data[i].name;
					}				
					that.teacherList = ret.data;
				}});
			},
			
			//获取详情
			getDetail(){
				var that = this;
				var arrangement_id = that.arrangement_id;
				if(arrangement_id == '') return false;
				
				this.$core.get({url:'fitness.manage/arrangement_detail',data:{arrangement_id:arrangement_id},success:(ret)=>{
					var data = ret.data;
					that.formData.type = data.course.courseCategory.name; //课程类型,
					that.formData.type_id = data.course.courseCategory.id;
					
					that.formData.name = data.course.name; //课程名称
					that.formData.course_id = data.course.id;
					
					that.formData.teacher_name = data.teacher?data.teacher.name:'';
					that.formData.teacher_id = data.teacher_id;
					
					that.formData.course_minutes = data.course.course_minutes; //课程时长（分）
					that.formData.room = data.classroom.name; //上课教室
					that.formData.classroom_id = data.classroom.id;
					that.formData.contain = data.course.people_count; //容纳人数（人）
					that.formData.color = data.course.courseColor.style; //课程颜色
					that.formData.degree = data.course.degree;
					
					that.classTime.push(data.time_list);
					that.getCourse();
				}});
			},
			
			//获取课程
			getCourse(){
				var that = this;
				let shop = this.$core.getCurrentShop();
				let shop_id = shop.id;
				var category_id = that.formData.type_id;
				if(shop_id == '' || category_id == '') return false;
				this.$core.get({url:'fitness.common/range_course',data:{shop_id:shop_id,course_category_id:category_id},success:(ret)=>{
					for(var i=0;i<ret.data.length;i++){
						ret.data[i].value = ret.data[i].id;
						ret.data[i].label = ret.data[i].name;
					}
					that.nameList = ret.data;
				}});
			},
			// 请选择课程类型
			typeConfirm(index) {
				this.formData.type = this.typeList[index].text;
				this.formData.type_id = this.typeList[index].id;
				this.getCourse();
			},
			// 请选择课程名称
			nameConfirm(e) {
				this.formData.name = e[0].label;
				var course = this.nameList;
				var course_id = e[0].value;
				for(var i=0;i<course.length;i++){
					if(course_id == course[i].id){
						this.formData.course_minutes = course[i].course_minutes;
						this.formData.contain = course[i].people_count;
						this.formData.color = course[i].course_color.style;
						this.formData.degree = course[i].degree;
						break;
					}
				}
				this.formData.course_id = course_id;
			},
			// 请选择上课教室
			roomConfirm(e) {
				this.formData.room = e[0].label;
				this.formData.classroom_id = e[0].value;
			},
			// 请选择上课教室
			teacherConfirm(e) {
				this.formData.teacher_name = e[0].label;
				this.formData.teacher_id = e[0].value;
			},
			colorSelect(index) {
				for (let i = 0; i < this.colorList.length; i++) {
					this.colorList[i].isSelect  = index==i?true:false
				}
				console.log(index,this.colorList)
			},
			colorConfirm() {
				this.colorList.map((val, index) => {
					if (val.isSelect) {
						this.formData.color = val.color;
					}
				})
				this.colorShow = false
			},
			// 请选择上课日期
			dateChange(e) {
				e.mmdd = e.result.substr(5,5)
				this.dateValue = e
				this.timeShow = true
			},
			// 请选择上课时间
			timeConfirm(e) {
				console.log(e)
				if (e[4].value < e[0].value || (e[4].value == e[0].value && e[6].value <= e[2].value)) {
					uni.showToast({
						icon: 'none',
						title: '结束时间不能小于开始时间'
					})
					return;
				}
				let time = '';
				let time_id = 0;
				e.map((val, index) => {
					time += val.label;
					time_id += val.value;
				});

				this.dateValue.time = time;
				this.dateValue.step_id = time_id;
				console.log(this.dateValue);
				this.classTime.push(this.dateValue);
				if(this.arrangement_id>0){
					this.addStepShow = false;;
				}
			},
			deleteTime(index) {
				this.classTime.splice(index,1);
				if(this.arrangement_id>0){
					this.addStepShow = true;
				}
			},
			delArrangement(){
				var that = this;
				var arrangement_id = that.arrangement_id;
				uni.showModal({
					title: '提示',
					content: '确定删除',
					success: function (res) {
						if (res.confirm) {
							that.$core.get({url:'fitness.common/del_arrangement',data:{arrangement_id:arrangement_id},success:(ret)=>{
								uni.showToast({
									title: ret.msg,
									icon: 'none',
									success() {
										uni.navigateBack({})
									}
								})
							}});
						}
					}
				})
				
			},
			submit() {
				var that = this;
				let shop = this.$core.getCurrentShop();
				that.$refs.uForm.validate(valid => {
					if (valid) {
						var arrangement_id = that.arrangement_id;
						var shop_id = shop.id;
						var course_id = that.formData.course_id;
						var classroom_id = that.formData.classroom_id;
						var teacher_id = that.formData.teacher_id;
						if(!classroom_id || !teacher_id || !course_id){
							uni.showToast({
								title: '请选择教室或老师或课程',
								icon:'none'
							})
							return false;
						}
						var data = [];
						var classTime = that.classTime;
						if(arrangement_id > 0 && classTime.length>1){
							uni.showToast({
								title: '编辑排课只能设置一个时间',
								icon:'none'
							})
							return false;
						}
						for(var i=0;i<classTime.length;i++){
							var time = classTime[i].time.split('至');
							
							data[i] = {date:classTime[i].result,start_time:time[0],end_time:time[1]};
						}
						//console.log(data);return false;
						if(data.length <= 0){
							uni.showToast({
								title: '请添加排课日期',
								icon:'none'
							})
							return false
						}
						let params = {teacher_id:teacher_id,arrangement_id:arrangement_id,shop_id:shop_id,course_id:course_id,classroom_id:classroom_id,data:data};
						//console.log(param);return;
						//先去查询是否有日期已有课程安排
						that.checkDate(params,function(){
							that.$core.post({url:'fitness.manage/add_arrangement',data:params,success:(ret)=>{
								uni.showToast({
									title: ret.msg,
									icon: 'none',
									success() {
										uni.navigateBack({})
									}
								})
							}});
						});
						
						
					} else {
						console.log('验证失败');
					}
				});
			},
			checkDate(params,cb){
				var that = this;
				this.$core.post({url:'fitness.manage/check_arrangement',data:params,success:(ret)=>{
					console.log(ret)
					if(ret.code == 1001){
						uni.showModal({
							title: '目标时间段已排课程',
							content: '确认覆盖？',
							success: function(res){
								if(res.confirm){
									cb()
								}
							}
						})
					}else{
						cb()
					}
				}});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 1rpx 0;
		background: #F6F6F6;
	}
	.form-wrapper {
		margin: 24rpx;
		background: #FFFFFF;
		border-radius: 24rpx;
		&__title {
			margin-bottom: 10rpx;
			line-height: 38rpx;
			font-size: 26rpx;
			color: #333333;
		}
		.color {
			display: inline-block;
			vertical-align: middle;
			margin-top: -6rpx;
			width: 100rpx;
			height: 48rpx;
			border-radius: 12rpx;
		}
		/deep/ .u-form-item {
			padding: 10rpx 30rpx;
			font-size: 26rpx;
		}
		/deep/ .u-input__input {
			text-align: right;
			font-size: 26rpx;
		}
		/deep/ .u-input__textarea {
			padding: 15rpx 0;
			line-height: 40rpx;
			box-sizing: border-box;
		}
		/deep/ .u-form-item__message {
			text-align: right;
		}
		/deep/ .u-input__right-icon--select {
			display: none;
		}
		/deep/ .u-list-item {
			// background: #FFFFFF;
		}
	}
	.order-footer {
		padding: 70rpx 24rpx;
		.order-footer__btn {
			margin-top: 20rpx;
		}
	}
	.color-wrapper {
		&__title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 80rpx;
			padding: 0 20rpx;
		}
		&__body {
			display: flex;
			flex-wrap: wrap;
			max-height: 60vh;
			padding: 10rpx;
			overflow-x: hidden;
			overflow-y: auto;
			-webkit-overflow-scrolling: touch;
			text {
				display: block;
				margin: 10rpx;
				width: 162rpx;
				height: 80rpx;
				border-radius: 24rpx;
				box-sizing: border-box;
				&.active {
					border: 4rpx #6D4BBE solid;
				}
			}
		}
	}
</style>